package dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import core.Dictionary;
import dto.ApproveDTO;

import util.Controller;

public class ApprovedDAO {
	
	public static boolean AddApproval(int WordID, int UserID){
		Connection cnn = null;
        CallableStatement cstat = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
        	String query = "insert into m_approve(m_wordID, m_userID) values(?, ?)";
            cnn = Controller.getConnection();
            pstmt = cnn.prepareStatement(query);
            pstmt.setInt(1, WordID);
            pstmt.setInt(1, UserID);
            pstmt.executeUpdate();
            
        } catch (Exception ex) {
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (cnn != null) {
                    cnn.close();
                }
            } catch (Exception ex) {
            }
        }

        return true;
    }
	public static boolean AddApproval(String WordID, String UserID){
		Connection cnn = null;
        CallableStatement cstat = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
        	String query = "insert into m_approve(m_wordID, m_userID) values(?, ?)";
            cnn = Controller.getConnection();
            pstmt = cnn.prepareStatement(query);
            pstmt.setString(1, WordID);
            pstmt.setString(1, UserID);
            pstmt.executeUpdate();
            
        } catch (Exception ex) {
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (cnn != null) {
                    cnn.close();
                }
            } catch (Exception ex) {
            }
        }

        return true;
    }
	public static boolean RemoveApproval(int m_id){
		Connection cnn = null;
        CallableStatement cstat = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
        	cnn= Controller.getConnection();
			pstmt = cnn.prepareStatement("delete from m_approve where m_id = ?");
	        pstmt.setInt(1, m_id);
	        pstmt.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (cnn != null) {
                    cnn.close();
                }
            } catch (Exception ex) {
            	return false;
            }
        }
		return true;

        
	}
	public static boolean RemoveApproval(String m_id){
		Connection cnn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
        	cnn= Controller.getConnection();
			pstmt = cnn.prepareStatement("delete from m_approve where m_id = ?");
	        pstmt.setString(1, m_id);
	        pstmt.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (cnn != null) {
                    cnn.close();
                }
            } catch (Exception ex) {
            	return false;
            }
        }
		return true;

        
	}
	public static ApproveDTO SelectApproval(int m_id){
		ApproveDTO record = new ApproveDTO();
		Connection cnn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            cnn= Controller.getConnection();
			pstmt = cnn.prepareStatement("select * from m_approve where m_id = ?");
			pstmt.setInt(1, m_id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				record.setM_id(rs.getInt("m_id"));
				record.setM_userID(rs.getInt("m_userID"));
				record.setM_wordID(rs.getInt("m_wordID"));
				
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		return record;
	}
}
